﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Video
{
    /// <summary>
    /// VideoPlayer.xaml 的交互逻辑
    /// </summary>
    [Obsolete("不建议使用")]
    public partial class VideoPlayer : UserControl
    {
        public VideoPlayer()
        {
            InitializeComponent();
            playerPlayButton.RelevanceMeidaElement = media;
            playerPlayButton.Checked += PlayerPlayButton_Checked;
            playerPlayButton.Unchecked += PlayerPlayButton_Unchecked;
            playerTimeShow.RelevanceMeidaElement = media;
            playerProgressbar.RelevanceMeidaElement = media;
            playerProgressbar.PreviewMouseLeftButtonUp += PlayerProgressbar_MouseLeftButtonUp;
            media.MediaOpened += Media_MediaOpened;
            media.MediaEnded += Media_MediaEnded;
            this.Loaded += VideoPlayer_Loaded;

        }

        private void PlayerPlayButton_Unchecked(object sender, RoutedEventArgs e)
        {
            _IsPlaying = false;
            PlayStateChanged?.Invoke(sender, EventArgs.Empty);
        }

        private void PlayerPlayButton_Checked(object sender, RoutedEventArgs e)
        {
            _IsPlaying = true;
            PlayStateChanged?.Invoke(sender, EventArgs.Empty);
        }

        private void PlayerProgressbar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            PositionChanged?.Invoke(this, EventArgs.Empty);
        }


        public event EventHandler PlayStateChanged;
        public event EventHandler PositionChanged;

        public double Volume
        {
            get { return media.Volume; }
            set { media.Volume = value; }
        }

        public TimeSpan Position
        {
            get { return media.Position; }
            set { media.Position = value; }
        }

        private bool _IsPlaying;

        public bool IsPlaying
        {
            get { return _IsPlaying; }
        }

        public void Play()
        {
            media.Play();
            _IsPlaying = true;
        }

        public void Pause()
        {
            media.Pause();
            _IsPlaying = false;
        }

        public void Stop()
        {
            media.Stop();
            _IsPlaying = false;
        }

        private void Media_MediaOpened(object sender, RoutedEventArgs e)
        {
            if (AutoPlay)
            {
                Action a = delegate { playerPlayButton.IsChecked = false; };
                if (!Dispatcher.CheckAccess())
                {
                    Dispatcher.Invoke(a);
                }
                else
                {
                    a();
                }

            }
        }

        private void Media_MediaEnded(object sender, RoutedEventArgs e)
        {
            Stop();
        }
        private void VideoPlayer_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private string _VideoFile;

        public string VideoFile
        {
            get { return _VideoFile; }
            set
            {
                _VideoFile = value;
                if (!string.IsNullOrEmpty(VideoFile))
                {
                    media.Source = new Uri(VideoFile, UriKind.Relative);
                }
                else
                {
                    media.Source = null;
                }
            }
        }

        private bool _AutoPlay;

        public bool AutoPlay
        {
            get { return _AutoPlay; }
            set { _AutoPlay = value; }
        }

    }
}
